package com.living.player.ui.recently.album;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.living.R;
import com.living.player.db.RecentlyManger;
import com.living.player.ui.recently.adapter.RecentlyAlbumAdapter;
import com.living.player.ui.recently.adapter.RecentlyAudioAdapter;
import com.md.album.db.SelfAlbumManager;
import com.md.basedpc.base.BaseFragment;
import com.md.basedpc.constant.BaseConstant;
import com.md.datastore.MysaveAlbumManager;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecentlyAlbumBatchFragment extends BaseFragment implements OnItemClickListener, View.OnClickListener {

    private static final String ALBUM_TYPE_CODE="ALBUM_TYPE_CODE";
    private TextView tv_batch_left, tv_batch_title, tv_batch_right;
    private Map<String, Album> mapSel = new ConcurrentHashMap<>();
    private RecentlyAlbumAdapter taskItemAdapter;
    private List<Album> albumList = new ArrayList<>();
    private int albumType;


    public static RecentlyAlbumBatchFragment newInstance(int albumType){
        Bundle bundle = new Bundle();
        bundle.putInt(ALBUM_TYPE_CODE,albumType);
        RecentlyAlbumBatchFragment fragment = new RecentlyAlbumBatchFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_player_recently_batch_delete;
    }

    public Map<String, Album> getMapSel() {
        return mapSel;
    }

    /**
     * 移除操作
     *
     * @param t
     */
    public void remove(Album t) {
        taskItemAdapter.remove(t);
        albumList.remove(t);
        refreshSelect();
        if (taskItemAdapter.getData().size() == 0) {
            getActivity().finish();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        albumType = getArguments().getInt(ALBUM_TYPE_CODE);

        tv_batch_left = view.findViewById(R.id.tv_batch_left);
        tv_batch_title = view.findViewById(R.id.tv_batch_title);
        tv_batch_right = view.findViewById(R.id.tv_batch_right);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        if (albumType == BaseConstant.SELF_CREATE_ALBUM){
            albumList = SelfAlbumManager.getIns().getAllSelfAlbum();
        }else if(albumType == BaseConstant.SAVE_ALBUM){
            albumList = MysaveAlbumManager.getIns().getAllSaveAlbum();
        } else {
            albumList = RecentlyManger.getInstance().getAllRecentAlbums();
        }

        taskItemAdapter = new RecentlyAlbumAdapter(true, mapSel);
        recyclerView.setAdapter(taskItemAdapter);

        taskItemAdapter.setOnItemClickListener(this);
        tv_batch_left.setOnClickListener(this);
        tv_batch_right.setOnClickListener(this);

        taskItemAdapter.setNewInstance(albumList);
        refreshSelect();
    }

    private void refreshSelect() {
        if (mapSel.size() == albumList.size()) {
            tv_batch_left.setText("取消全选");
        } else {
            tv_batch_left.setText("全选");
        }
        if (mapSel.size() > 0) {
            tv_batch_title.setText("已选定" + mapSel.size() + "张");
        } else {
            tv_batch_title.setText("批量操作");
        }
        if (onBatchSelectLisenter != null) {
            onBatchSelectLisenter.onSelect(mapSel.size(), albumList.size());
        }
    }

    @Override
    public void onClick(View v) {
        int _id = v.getId();
        if (_id == R.id.tv_batch_left) {
            if (mapSel.size() == albumList.size()) {
                mapSel.clear();
            } else {
                // 全选
                for (Album tasksManagerModel : albumList) {
                    mapSel.put(tasksManagerModel.getId(), tasksManagerModel);
                }
            }
            taskItemAdapter.notifyDataSetChanged();
            refreshSelect();
        } else if (_id == R.id.tv_batch_right) {
            getActivity().finish();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Album tasksManagerModel = taskItemAdapter.getItem(position);
        if (mapSel.containsKey(tasksManagerModel.getId())) {
            mapSel.remove(tasksManagerModel.getId());
        } else {
            mapSel.put(tasksManagerModel.getId(), tasksManagerModel);
        }
        taskItemAdapter.notifyItemChanged(position);
        refreshSelect();
    }

    OnBatchSelectLisenter onBatchSelectLisenter;

    public void setOnBatchSelectLisenter(OnBatchSelectLisenter onBatchSelectLisenter) {
        this.onBatchSelectLisenter = onBatchSelectLisenter;
    }

    public interface OnBatchSelectLisenter {
        void onSelect(int count, int total);
    }


}
